﻿using AutoMapper;
using B.S.DataBase.MyProject.Domain;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using B.S.DataBase.MyRead.API.Appicant.Command.RoleMenu;
using B.S.DataBase.MyRead.API.DTOs;
using MediatR;

namespace B.S.DataBase.MyRead.API.Appicant.CommandHandler.RoleMenu
{
    public class GetMenuNameCommandHandler : IRequestHandler<GetMenuNameCommand, APIResult<List<PerposDto>>>
    {

        private readonly IBaseRepository<Menu> menuRepository;
        private readonly IMapper mapper;

        public GetMenuNameCommandHandler(IBaseRepository<Menu> menuRepository, IMapper mapper )
        {
            this.menuRepository = menuRepository;
            this.mapper = mapper;
        }


        public Task<APIResult<List<PerposDto>>> Handle(GetMenuNameCommand request, CancellationToken cancellationToken)
        {
            APIResult<List<PerposDto>> result = new APIResult<List<PerposDto>>();
            var model = menuRepository.Getall();
            result.Code =APIEnum.查询成功;
            result.Msg = "成功";
            result.data = GetTree(0);
            return  Task.FromResult(result);
        }



        public List<PerposDto> GetTree(int MenuId)
        {
            var depars = menuRepository.Getall().Where(x=>x.PId == MenuId);
            var dot = mapper.Map<List<PerposDto>>(depars);
            foreach (var item in dot)
            {
                item.perposDtos = GetTree(item.MenuId);
            }
            return dot;
        }



    }
}
